home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
53003
/
53003.xpi
/
chrome
/
flashVideoDownload.jar
/
content
/
overlay.js
next >
Wrap
Text File
|
2009-12-04
|
12KB
|
336 lines
var flashVideoDownload;
flashVideoDownload = new Object();
flashVideoDownload.initialization = function()
{
window.addEventListener("load", flashVideoDownload.onWindowLoad, false);
window.addEventListener("unload", flashVideoDownload.onWindowUnload, false);
}
flashVideoDownload.onWindowLoad = function()
{
var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
observerService.addObserver(flashVideoDownload.flashVideoDownloadObserver, "http-on-examine-response", false);
observerService.addObserver(flashVideoDownload.flashVideoDownloadObserver, "http-on-modify-request", false);
observerService.addObserver(flashVideoDownload.flashVideoDownloadObserver, "http-on-examine-cached-response", false);
var listener = {
onStatusChange: function() {},
onStateChange: function() {},
onLocationChange: function(aProgress, aRequest, aURI) {
flashVideoDownload.clearStatusBar();
if (aURI==null) {
document.getElementById("flashVideoDownloadStatusBarIcon").setAttribute("class", "flashVideoDownloadIconDisabled");
} else {
flashVideoDownload.setStatusBar(getBrowser().selectedBrowser.contentDocument)
}
},
onProgressChange: function() {},
onSecurityChange: function() {}
};
getBrowser().addProgressListener(listener, Components.interfaces.nsIWebProgress.NOTIFY_LOCATION);
}
flashVideoDownload.onWindowUnload = function()
{
var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
observerService.removeObserver(flashVideoDownload.flashVideoDownloadObserver, "http-on-examine-response", false);
observerService.removeObserver(flashVideoDownload.flashVideoDownloadObserver, "http-on-modify-request", false);
observerService.removeObserver(flashVideoDownload.flashVideoDownloadObserver, "http-on-examine-cached-response", false);
}
flashVideoDownload.buttonPressed = function(e)
{
var popup = document.getElementById("flashVideoDownloadContextMenu");
popup.showPopup(e.target, -1, -1, "popup", "bottomleft", "topleft");
}
flashVideoDownload.flashVideoDownloadObserver =
{
observe: function(aSubject, aTopic, aData) {
if ((aTopic=="http-on-examine-response") || (aTopic=="http-on-modify-request")
|| (aTopic=="http-on-examine-cached-response")){
try {
aSubject.QueryInterface(Components.interfaces.nsIRequest);
var url = aSubject.name;
var contentType = aSubject.contentType;
var contentLength = aSubject.contentLength;
var fileType = flashVideoDownload.getFileType(contentType, url);
aSubject.QueryInterface(Components.interfaces.nsIChannel);
if ((fileType=="swf") || (fileType=="flv") || (fileType=="mp4")) {
var browser = flashVideoDownload.getBrowser(aSubject, aSubject);
var doc = browser.contentDocument;
var title = doc.title;
var domain = doc.domain;
var href = doc.location.href;
flashVideoDownload.addFile(doc, {
title : title,
domain : domain,
href : href,
fileType : fileType,
contentType : contentType,
contentLength : contentLength,
url : url,
doc : doc,
icon : flashVideoDownload.getIcon(doc)
});
// dumb(contentType+" "+url);
}
} catch(ex) {
}
} else {
}
return true
},
QueryInterface: function(aIID) {
if (aIID.equals(Components.interfaces.nsISupports) ||
aIID.equals(Components.interfaces.nsIObserver) )
return this;
throw Components.results.NS_NOINTERFACE;
}
}
flashVideoDownload.getIcon = function (doc)
{
var favIcon = "";
var linkEl = doc.getElementsByTagName("link");
for (var i=0;i<linkEl.length;i++) {
if (linkEl[i].getAttribute("rel")) {
if (linkEl[i].getAttribute("rel").toLowerCase()=="shortcut icon") {
favIcon = linkEl[i].href
break;
}
}
}
return favIcon;
}
flashVideoDownload.getBrowser = function(aChannel, aSubject)
{
try {
var notificationCallbacks =
aChannel.notificationCallbacks ? aChannel.notificationCallbacks : aSubject.loadGroup.notificationCallbacks;
if (!notificationCallbacks)
return null;
var callback = notificationCallbacks.getInterface(Components.interfaces.nsIDOMWindow);
return callback.top.document ?
gBrowser.getBrowserForDocument(callback.top.document) : null;
}
catch(e) {
return null;
}
}
flashVideoDownload.addFile = function(doc, params)
{
if (doc.swfList==null) doc.swfList = new Array();
if (doc.flvList==null) doc.flvList = new Array();
if (params.fileType=="swf") flashVideoDownload.addToList(doc.swfList ,params);
if ((params.fileType=="flv") || (params.fileType=="mp4")) flashVideoDownload.addToList(doc.flvList ,params);
flashVideoDownload.setStatusBar(doc);
}
flashVideoDownload.addToList = function(list, params)
{
for (var i=0;i<list.length;i++) {
if (list[i].url==params.url) return false;
}
list.push(params);
}
flashVideoDownload.clearStatusBar = function()
{
var flashVideoDownloadContextMenu = document.getElementById("flashVideoDownloadContextMenu");
while (flashVideoDownloadContextMenu.firstChild) {
flashVideoDownloadContextMenu.removeChild(flashVideoDownloadContextMenu.firstChild);
}
}
flashVideoDownload.setStatusBar = function(doc)
{
if (getBrowser().selectedBrowser.contentDocument!=doc) return false;
if (doc.swfList==null) doc.swfList = new Array();
if (doc.flvList==null) doc.flvList = new Array();
var flashVideoDownloadContextMenu = document.getElementById("flashVideoDownloadContextMenu");
while (flashVideoDownloadContextMenu.firstChild) {
flashVideoDownloadContextMenu.removeChild(flashVideoDownloadContextMenu.firstChild);
}
if (doc.swfList.length>0) {
for (var i=0;i<doc.swfList.length;i++) {
var menuItem = flashVideoDownload.createMenuItem(doc.swfList[i]);
flashVideoDownloadContextMenu.appendChild(menuItem);
}
}
if (doc.flvList.length>0) {
if (doc.swfList.length>0) {
var separator = document.createElementNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul", "xul:menuseparator");
flashVideoDownloadContextMenu.appendChild(separator);
}
if (doc.flvList.length>0) {
var menuitem = document.createElementNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul", "xul:menuitem");
menuitem.setAttribute("label", "Videos to Dwonload");
menuitem.style.fontWeight = "bold"
flashVideoDownloadContextMenu.appendChild(menuitem);
}
for (var i=0;i<doc.flvList.length;i++) {
var menuItem = flashVideoDownload.createMenuItem(doc.flvList[i]);
menuItem.setAttribute("class", "menuitem-iconic");
flashVideoDownloadContextMenu.appendChild(menuItem);
}
}
if ((doc.swfList.length>0) || (doc.flvList.length>0)) {
if (doc.flvList.length>0) {
document.getElementById("flashVideoDownloadStatusBarIcon").setAttribute("class", "flashVideoDownloadIconEnabledVideo");
} else {
document.getElementById("flashVideoDownloadStatusBarIcon").setAttribute("class", "flashVideoDownloadIconEnabled");
}
} else {
document.getElementById("flashVideoDownloadStatusBarIcon").setAttribute("class", "flashVideoDownloadIconDisabled");
}
}
flashVideoDownload.createMenuItem = function(params)
{
var fileType = params.fileType;
var label = params.url;
if ((fileType=="flv") || (fileType=="mp4")) {
label = params.title + "." + fileType;
} else {
var dotPos = label.lastIndexOf("." + fileType)
if (dotPos!=-1) {
var str = label.substr(1, dotPos+3);
var pos = str.lastIndexOf("/");
if (pos!=-1) {
var label = str.substr(pos+1);
if (label) {
}
}
}
}
var menuitem = document.createElementNS("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul", "xul:menuitem");
menuitem.setAttribute("type", params.fileType);
menuitem.setAttribute("label", label);
menuitem.setAttribute("url", params.url);
menuitem.setAttribute("tooltiptext", params.url);
menuitem.setAttribute("image", params.icon);
menuitem.addEventListener("click", function(e) {
var t = e.currentTarget;
flashVideoDownload.downloadFile(t.getAttribute("label"), t.getAttribute("url"), t.getAttribute("type"));
},false);
menuitem.addEventListener("mouseover", function(e) {
var t = e.currentTarget;
flashVideoDownload.showHideElement(params.doc, t.getAttribute("url"), true);
},false);
menuitem.addEventListener("mouseout", function(e) {
var t = e.currentTarget;
flashVideoDownload.showHideElement(params.doc, t.getAttribute("url"), false);
},false);
return menuitem;
}
flashVideoDownload.getElPos = function(domElement)
{
var position = {
x: 0,
y: 0
}
while ((domElement.offsetParent) && (domElement.tagName.toLowerCase()!='body')) {
position.x += domElement.offsetLeft;
position.y += domElement.offsetTop;
domElement = domElement.offsetParent;
}
position.x += domElement.offsetLeft;
position.y += domElement.offsetTop;
return position;
}
flashVideoDownload.showHideElement = function(doc, src, isShow)
{
var embedList = doc.getElementsByTagName("embed");
if (embedList.length>0) {
for (var i=0;i<embedList.length;i++) {
if (embedList[i].src==src) {
if (isShow) {
var _body = doc.getElementsByTagName("body")[0];
if (doc.floatElement==null) {
doc.floatElement = doc.createElement("div");
_body.appendChild(doc.floatElement);
}
var div = doc.floatElement;
div.style.position = "absolute";
div.style.zIndex = "10000"
var pos = flashVideoDownload.getElPos(embedList[i]);
div.style.left = (pos.x-4) + "px";
div.style.top = (pos.y-4) + "px";
div.style.width = (embedList[i].clientWidth) + "px";
div.style.height = (embedList[i].clientHeight) + "px";
div.style.border = "4px solid #ff0000"
div.style.display='';
} else {
doc.floatElement.style.display='none';
}
}
}
}
}
flashVideoDownload.getDownloadFile = function(defaultString, fileType)
{
var nsIFilePicker = Components.interfaces.nsIFilePicker;
var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
fp.init(window, "Save As", nsIFilePicker.modeSave);
try {
var urlExt = defaultString.substr(defaultString.lastIndexOf(".")+1, 3);
if (urlExt!=fileType) defaultString += "." + fileType
}catch(ex){}
fp.defaultString = defaultString;
fp.appendFilter(fileType, "*." + fileType);
var rv = fp.show();
if (rv == nsIFilePicker.returnOK || rv == nsIFilePicker.returnReplace) {
var file = fp.file;
var path = fp.file.path;
return file;
}
return null;
}
flashVideoDownload.downloadFile = function(title, url, fileType)
{
var file = flashVideoDownload.getDownloadFile(title, fileType);
var persist = Components.classes['@mozilla.org/embedding/browser/nsWebBrowserPersist;1'].createInstance(Components.interfaces.nsIWebBrowserPersist);
var ios = Components.classes['@mozilla.org/network/io-service;1'].getService(Components.interfaces.nsIIOService);
var uri = ios.newURI(url, null, null);
var target = ios.newFileURI(file)
var xfer = Components.classes["@mozilla.org/transfer;1"]
.createInstance(Components.interfaces.nsITransfer);
xfer.init(uri, target, "", null, null, null, persist);
persist.progressListener = xfer;
persist.saveURI(uri, null, null, null, null, file);
}
flashVideoDownload.getFileType = function(contentType, url)
{
if (contentType=="application/x-shockwave-flash") {
return "swf";
}
if ((contentType=="video/x-flv") || (contentType=="video/flv")) {
return "flv";
} else {
var urlExt = url.substr(url.lastIndexOf(".")+1, 3);
if (urlExt=="flv") {
return "flv";
}
}
if (contentType=="video/mp4") {
return "mp4";
}
return "";
}
flashVideoDownload.initialization();